package com.atguigu.serviceBase.handler;

import com.atguigu.common.Result;
import com.atguigu.serviceBase.exception.GuliException;
import com.baomidou.mybatisplus.extension.api.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Summerday
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    //指定出现什么异常执行这个方法
    @ExceptionHandler(Exception.class)
    @ResponseBody //为了返回数据
    public Result error(Exception e) {
        log.error(e.getMessage());
        return Result.error().message(e.getMessage());
    }


    @ExceptionHandler(GuliException.class)
    @ResponseBody
    public Result guLiError(Exception e) {
        log.error(e.getMessage());
        return Result.error().message(e.getMessage());
    }

}
